// Copyright (c) 2022 Huawei Technologies Co., Ltd
// Copyright (c) 2019, Facebook CORPORATION.
// All rights reserved.
//
// Licensed under the BSD 3-Clause License  (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "ATen/native/npu/utils/OpAdapter.h"
#include "ATen/native/npu/utils/CalcuOpUtil.h"

namespace at {
namespace native {
using namespace at::native::npu;

Tensor& floor_divide_out_npu_nocheck(Tensor& result, const Tensor& self, Scalar other) {
  OpCommand cmd;
  cmd.Name("FloorDiv")
        .Input(self)
        .Input(other, self.scalar_type())
        .Output(result)
        .Run();
  return result;
}

Tensor& floor_divide_out_scalar_npu(const Tensor& self, Scalar other, Tensor& result) {
  Tensor formatCastOfSelf = OpPreparation::CastBackToOriFormat(self);
  auto outputSize = formatCastOfSelf.sizes();
  OpPreparation::CheckOut(
      {self},
      result,
      ACL_FORMAT_ND,
      result.scalar_type(),
      outputSize);

  floor_divide_out_npu_nocheck(result, formatCastOfSelf, other);
  return result;
}

Tensor& floor_divide_out_tensor_npu(const Tensor& self, const Tensor& other, Tensor& result) {
  Tensor formatCastOfSelf = OpPreparation::CastBackToOriFormat(self);
  auto outputSize = formatCastOfSelf.sizes();
  OpPreparation::CheckOut(
      {self, other},
      result,
      ACL_FORMAT_ND,
      result.scalar_type(),
      outputSize);
  // executing the NPU operator
  if (other.dim() == 0) {
    floor_divide_out_npu_nocheck(result, self, other.item());
  } else {
    OpCommand cmd;
    cmd.Name("FloorDiv")
        .Input(self)
        .Input(other)
        .Output(result)
        .Run();
  }

  return result;
}

Tensor floor_divide_tensor_npu(const Tensor& self, const Tensor& other) {
  Tensor selfCast = self;
  if(self.dtype() == ScalarType::Bool){
    selfCast = selfCast.to(ScalarType::Float);
  }
  Tensor otherCast = other;
  if (other.scalar_type() == ScalarType::Double) {
    otherCast = otherCast.to(ScalarType::Float);
  }
  if (other.scalar_type() == ScalarType::Long) {
    otherCast = otherCast.to(ScalarType::Int);
  }

  // calculate the output size
  bool isSelfWrapped = CalcuOpUtil::is_scalar_wrapped_to_tensor(selfCast);
  Tensor outputTensor = isSelfWrapped ? otherCast : selfCast;

  auto outputSize = broadcast_ops_npu_output_size(selfCast, otherCast);

  // construct the output tensor of the NPU

  Tensor result = OpPreparation::ApplyTensorWithFormat(
      outputSize,
      outputTensor.options(),
      CalcuOpUtil::get_tensor_npu_format(selfCast));

  // calculate the output result of the NPU
  floor_divide_out_tensor_npu(selfCast, otherCast, result);

  return result;
}

Tensor floor_divide_scalar_npu(const Tensor& self, Scalar other) {

    // calculate the output size
    auto outputSize = input_same_output_size(self);

    // construct the output tensor of the NPU
    Tensor result = OpPreparation::ApplyTensor(self, outputSize);

    // calculate the output result of the NPU
    floor_divide_out_scalar_npu(self, other, result);

    return result;
}

Tensor& floor_divide_tensor_npu_(Tensor& self, const Tensor& other) {
    Tensor otherCast = other;
    if (other.scalar_type() == ScalarType::Double) {
      otherCast = otherCast.to(ScalarType::Float);
    }
    if (other.scalar_type() == ScalarType::Long) {
      otherCast = otherCast.to(ScalarType::Int);
    }
    SmallVector<Tensor, N> inputs = {self, otherCast};
    SmallVector<Tensor, N> outputs = {self};
    CalcuOpUtil::check_memory_over_laps(inputs, outputs);

    if (!NpuUtils::check_match(&self)) {
      Tensor contiguousSelf = NpuUtils::format_contiguous(self);
      Tensor result = floor_divide_out_tensor_npu(contiguousSelf, other, contiguousSelf);
      NpuUtils::format_fresh_view(self, result);
    } else {
      floor_divide_out_tensor_npu(self, otherCast, self);
    }

    return self;
}

Tensor& floor_divide_scalar_npu_(Tensor& self, Scalar other) {
    if (!NpuUtils::check_match(&self)) {
      Tensor contiguousSelf = NpuUtils::format_contiguous(self);
      floor_divide_out_scalar_npu(contiguousSelf, other, contiguousSelf);
      NpuUtils::format_fresh_view(self, contiguousSelf);
    } else {
      floor_divide_out_scalar_npu(self, other, self);
    }
    return self;
}

TORCH_LIBRARY_IMPL(aten, NPU, m) {
  m.impl("floor_divide", TORCH_FN(floor_divide_tensor_npu));
  m.impl("floor_divide.Scalar", TORCH_FN(floor_divide_scalar_npu));
  m.impl("floor_divide_.Tensor", TORCH_FN(floor_divide_tensor_npu_));
  m.impl("floor_divide_.Scalar", TORCH_FN(floor_divide_scalar_npu_));
  m.impl("floor_divide.out", TORCH_FN(floor_divide_out_tensor_npu));
}

} // namespace native
}